import {
  Body,
  Controller,
  Get,
  Param,
  Headers,
  Query,
  Post,
  Put,
  Delete,
  UseInterceptors,
  ClassSerializerInterceptor,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { ILike } from 'typeorm';
import { IsAuth } from 'src/decorators/auth.decorator';
import { IResponseData, responseData } from 'src/utils/result';
import { DictTypeService } from './dictType.service';
import {
  CreateDictTypeDto,
  FindDictTypeListDto,
  DictTypeDto,
  UpdateDictTypeDto,
} from './dictType.dto';
@ApiTags('字典类型模块')
@ApiBearerAuth()
@Controller('dictType')
export class DictTypeController {
  constructor(private readonly dictTypeService: DictTypeService) {}
  @ApiOperation({ summary: '查询字典类型列表' })
  @ApiResponse({
    status: 200,
    description: '查询字典类型列表',
    type: DictTypeDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get()
  @IsAuth('system:dict:list')
  async findList(
    @Query() dto: FindDictTypeListDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictTypeService.getLists({
      where: {
        ...dto,
        dict_name: dto.dict_name ? ILike('%' + dto.dict_name + '%') : null,
        dict_type: dto.dict_type ? ILike('%' + dto.dict_type + '%') : null,
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询所有字典类型' })
  @ApiResponse({
    status: 200,
    description: '查询所有字典类型',
    type: DictTypeDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get('findAll')
  async findAll(@Headers() headers): Promise<IResponseData> {
    const res = await this.dictTypeService.getAll();
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @ApiOperation({ summary: '查询字典类型详情' })
  @ApiResponse({
    status: 200,
    description: '查询字典类型详情',
    type: DictTypeDto,
  })
  @UseInterceptors(ClassSerializerInterceptor)
  @Get(':id')
  @IsAuth('system:dict:detail')
  async find(
    @Param('id') id: number,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictTypeService.getOne({
      where: {
        id,
      },
    });
    return responseData({
      code: 100010,
      data: res,
      language: headers['language'],
    });
  }

  @Post()
  @IsAuth('system:dict:add')
  @ApiOperation({ summary: '创建字典类型' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async create(
    @Body() dto: CreateDictTypeDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictTypeService.create(dto);
    return responseData({
      code: 100020,
      data: res,
      language: headers['language'],
    });
  }

  @Put()
  @IsAuth('system:dict:update')
  @ApiOperation({ summary: '更新字典类型' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async update(
    @Body() dto: UpdateDictTypeDto,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictTypeService.update(dto);
    return responseData({
      code: 100030,
      data: res,
      language: headers['language'],
    });
  }

  @Delete()
  @IsAuth('system:dict:delete')
  @ApiOperation({ summary: '删除字典类型' })
  @ApiResponse({ status: 200, description: '操作成功' })
  async delete(
    @Query('ids') query: string,
    @Headers() headers,
  ): Promise<IResponseData> {
    const res = await this.dictTypeService.delete(query);
    return responseData({
      code: 100040,
      data: res,
      language: headers['language'],
    });
  }
}
